package com.ziyia.navigationview;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    // 下拉刷新
    SwipeRefreshLayout swipeRefreshLayout = null;
    ElementAdapter elementAdapter = null;

    // 卡片数据
    List<Element> elements = new ArrayList<>();
    RecyclerView recyclerView = null;

    DrawerLayout drawerLayout = null;
    FloatingActionButton floatingActionButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        cardDataAdd();
        Toolbar toolbar = findViewById(R.id.toolbar);
        // 设置标题内容
        toolbar.setTitle("标题");
        toolbar.setTitleTextColor(0xff0000ff);
        // 设置子标题
        toolbar.setSubtitle("子标题");
        // 设置颜色 0x 两位透明值 六位颜色值
        toolbar.setSubtitleTextColor(0xff00ff00);
        toolbar.setNavigationIcon(R.drawable.ic_baseline_format_list_bulleted_24);
        setSupportActionBar(toolbar);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }

        NavigationView navigationView = findViewById(R.id.navView);
        navigationView.setCheckedItem(R.id.navCall);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull @org.jetbrains.annotations.NotNull MenuItem item) {
                drawerLayout.closeDrawers();
                return true;
            }
        });


        // floatingActionButton
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar.make(v, "嗯？", Snackbar.LENGTH_SHORT)
                        // setAction不是必须的。
                        .setAction("Undo", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(MainActivity.this, "点击事件", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });


        // 卡片布局渲染
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(gridLayoutManager);
        elementAdapter = new ElementAdapter(this, elements);
        recyclerView.setAdapter(elementAdapter);

        // 下拉刷新
        swipeRefreshLayout.setColorSchemeResources(R.color.green);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void  onRefresh() {
                if (elements.size() > 0) {
                    elements.remove(0);
                    elementAdapter.notifyDataSetChanged();
                    swipeRefreshLayout.setRefreshing(false);
                } else {
                    refreshElements();
                }

            }
        });

    }

    private void cardDataAdd() {
        elements.clear();
        Random random = new Random(1000);
        for (int i = 0; i < 20; i++) {
            i += random.nextInt();
            if (i % 2 != 0) {
                elements.add(new Element(i + "年少有为", R.drawable.user));
            }
            elements.add(new Element(getClass().getName(), R.drawable.user));

        }
    }


    private void refreshElements() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                    try {
                        Thread.sleep(2000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                cardDataAdd();
                                elementAdapter.notifyDataSetChanged();
                                // 关闭下拉菜单
                                swipeRefreshLayout.setRefreshing(false);
                                Toast.makeText(MainActivity.this, "haha", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

//                    Toast.makeText(MainActivity.this, "至少保留两个", Toast.LENGTH_SHORT).show();




            }
        }).start();
    }


    private void init() {
        recyclerView = findViewById(R.id.recyview);
        drawerLayout = findViewById(R.id.drawerLayout);
        floatingActionButton = findViewById(R.id.fab);
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }
}
